package com.example.demo.config;

import com.example.demo.job.ContractInspectJob;
import com.example.demo.job.LateFeeJob;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Quartz 定时任务配置
 *
 * @author Ethan Blake
 * @version 1.0
 * @since 2024/8/27
 */
@Configuration
public class QuartzConfiguration {

    private final String defaultGroup = "defaultGroup";

    @Bean
    public JobDetail contractInspectJobDetail() {
        // storeDurably() 的主要作用是使一个 `JobDetail` 可以在没有任何关联的 `Trigger`
        // 的情况被保存到 Quartz 的 `JobStore` 中（无论是内存还是数据库）
        // 这意味着即使当前没有任何 Trigger 计划触发这个 JobDetail，Quartz 也不会自动删除它
        return JobBuilder.newJob(ContractInspectJob.class)
                .withIdentity("contractInspectJobDetail", defaultGroup)
                .withDescription("合同检查")
                .storeDurably()
                .build();
    }

    @Bean
    public JobDetail lateFeeJobDetail() {
        return JobBuilder.newJob(LateFeeJob.class)
                .withIdentity("lateFeeJobDetail", defaultGroup)
                .withDescription("滞纳金生成")
                .storeDurably()
                .build();
    }
}
